package cn.dong.edu.controller.api;


import cn.dong.api.domain.CommonResult;
import cn.dong.api.domain.ResultCodeEnum;
import cn.dong.api.utils.ExceptionUtils;
import cn.dong.api.utils.JwtInfo;
import cn.dong.api.utils.JwtUtils;
import cn.dong.base.dto.CollectDto;
import cn.dong.base.handler.CustomException;
import cn.dong.edu.domain.CourseCollect;
import cn.dong.edu.service.CourseCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-24
 */
@RestController
@Slf4j
@RequestMapping("/api/edu/course-collect")
public class ApiCourseCollectController {

    @Autowired
    private CourseCollectService courseCollectService;

    /**
     * 创建收藏
     *
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("/auth/create/{courseId}")
    public CommonResult create(@PathVariable String courseId,
                               HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        CourseCollect collect = new CourseCollect();
        collect.setCourseId(courseId);
        collect.setMemberId(jwtInfo.getId());
        // 用户收藏的唯一索引，有可能用户多次点击收藏
        try {
            courseCollectService.save(collect);
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
        }
        return CommonResult.ok();
    }


    /**
     * 移除收藏
     *
     * @return
     */
    @DeleteMapping("/auth/delete/{courseId}")
    public CommonResult delete(@PathVariable("courseId") String courseId,  HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        // 查询是否收藏
        QueryWrapper<CourseCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("member_id", jwtInfo.getId());
        courseCollectService.remove(queryWrapper);
        return CommonResult.ok();
    }

    /**
     * 根据用户id，查询这个用户所有的收藏
     * @param request
     * @return
     */
    @GetMapping("/auth/findAllByMemberId")
    public CommonResult findAllByMemberId(HttpServletRequest request){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        List<CollectDto> list = courseCollectService.findAllByMemberId(jwtInfo.getId());
        return CommonResult.ok().data(list);
    }

    /**
     * 是否收藏了该课程
     * @return
     */
    @GetMapping("/auth/ifCollect/{courseId}")
    public CommonResult ifCollect(@PathVariable("courseId") String courseId,
                                  HttpServletRequest request){
        // 判断是否登录
        JwtInfo info = JwtUtils.getMemberIdByJwtToken(request);
        if (info == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        // 查询是否收藏
        QueryWrapper<CourseCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("member_id", info.getId());
        int count = courseCollectService.count(queryWrapper);
        // 未收藏
        if (count == 0) {
            return CommonResult.ok().data(false);
        }
        // 已经收藏
        return CommonResult.ok().data(true);
    }


}

